package com.clk.portfoy.screens;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.border.EmptyBorder;

import com.clk.desktop.gui.components.basic.ClkButton;
import com.clk.desktop.gui.components.basic.ClkFrame;
import com.clk.desktop.gui.components.basic.ClkLabel;
import com.clk.portfoy.PortfoyStatics;
import com.clk.portfoy.complib.piyasa.ClkPiyasaPanel;
import com.clk.portfoy.complib.portfoy.takip.ClkPortfoyTakipPanel;
import com.swtdesigner.SwingResourceManager;

public class ScrMain extends ClkFrame {

	private JMenuItem newItemMenuItem_1;
	private JMenuItem menuDegerIzleme;
	private JMenuItem menuPortfoyDegerlendirme;
	private JMenu portfoyDetayMenu;
	private ClkButton btnKapat;
	private ClkLabel lblClkLogo;
	private ClkPiyasaPanel clkPiyasaPanel;
	private JMenuItem mItemHakkinda;
	private JMenu yardimMenu;
	private JMenuItem newItemMenuItem;
	private JMenu menuEdit;
	private JMenuBar menuBar;

	private class NewItemMenuItemActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_newItemMenuItem_actionPerformed(e);
		}
	}

	private class MItemHakkindaActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_mItemHakkinda_actionPerformed(e);
		}
	}

	private class BtnKapatActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_btnKapat_actionPerformed(e);
		}
	}

	private class MenuPortfoyDegerlendirmeActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_menuPortfoyDegerlendirme_actionPerformed(e);
		}
	}

	private class MenuDegerIzlemeActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_menuDegerIzleme_actionPerformed(e);
		}
	}

	private class NewItemMenuItem_1ActionListener implements ActionListener {
		public void actionPerformed(final ActionEvent e) {
			do_newItemMenuItem_1_actionPerformed(e);
		}
	}
	private class ThisWindowListener extends WindowAdapter {
		public void windowClosing(final WindowEvent e) {
			do_this_windowClosing(e);
		}
	}

	private ClkPortfoyTakipPanel clkPortfoyPanel;
	Timer timer = new Timer();
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// ---------------------------------------------------------------------------------
	public ScrMain() {
		super();
		this.setLocation(0, 0);
		initGUI();
		setSize(new Dimension(760, 347));
		lblClkLogo.setIcon(PortfoyStatics.imgCLK);
		btnKapat.setIcon(PortfoyStatics.imgClose);
		PortfoyStatics.PORTFOY.register(clkPortfoyPanel);
		PortfoyStatics.PIYASA.register(clkPiyasaPanel);
		timer.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				System.out.println("******************Timer Tick*****************************************");
				refresh();
			}
		}, 30000, 30000);
	}

	// ---------------------------------------------------------------------------------
	private void initGUI() {
		setUndecorated(false);
		setIconImage(SwingResourceManager.getImage(ScrMain.class, "/com/clk/portfoy/images/CLK1.0.png"));
		setResizable(false);
		setTitle("Clk-Portfoy");
		addWindowListener(new ThisWindowListener());
		{
			clkPortfoyPanel = new ClkPortfoyTakipPanel();
			clkPortfoyPanel.setBounds(10, 79, 734, 207);
			getContentPane().add(clkPortfoyPanel);
			{
				btnKapat = new ClkButton();
				btnKapat.setBounds(653, 21, 60, 63);
				clkPortfoyPanel.add(btnKapat);
				btnKapat.addActionListener(new BtnKapatActionListener());
				btnKapat.setContentAreaFilled(false);
				btnKapat.setBorder(new EmptyBorder(0, 0, 0, 0));
				btnKapat.setBorderPainted(false);
			}
		}
		{
			menuBar = new JMenuBar();
			setJMenuBar(menuBar);
			{
				portfoyDetayMenu = new JMenu();
				portfoyDetayMenu.setText("Pencereler");
				menuBar.add(portfoyDetayMenu);
				{
					menuPortfoyDegerlendirme = new JMenuItem();
					menuPortfoyDegerlendirme.addActionListener(new MenuPortfoyDegerlendirmeActionListener());
					menuPortfoyDegerlendirme.setText("Portfoy Degerlendirmesi");
					portfoyDetayMenu.add(menuPortfoyDegerlendirme);
				}
				{
					menuDegerIzleme = new JMenuItem();
					menuDegerIzleme.addActionListener(new MenuDegerIzlemeActionListener());
					menuDegerIzleme.setText("Deger Izleme");
					portfoyDetayMenu.add(menuDegerIzleme);
				}
			}
			{
				menuEdit = new JMenu();
				menuEdit.setText("Duzenle");
				menuBar.add(menuEdit);
				{
					newItemMenuItem = new JMenuItem();
					newItemMenuItem.addActionListener(new NewItemMenuItemActionListener());
					newItemMenuItem.setText("Portfoye Ekle/Cikart");
					menuEdit.add(newItemMenuItem);
				}
				{
					newItemMenuItem_1 = new JMenuItem();
					newItemMenuItem_1.addActionListener(new NewItemMenuItem_1ActionListener());
					newItemMenuItem_1.setText("Izlediklerime Ekle/Cikar");
					menuEdit.add(newItemMenuItem_1);
				}
			}
			{
				yardimMenu = new JMenu();
				yardimMenu.setText("Yardim");
				menuBar.add(yardimMenu);
				{
					mItemHakkinda = new JMenuItem();
					mItemHakkinda.addActionListener(new MItemHakkindaActionListener());
					mItemHakkinda.setText("Hakkinda");
					yardimMenu.add(mItemHakkinda);
				}
			}
		}
		{
			clkPiyasaPanel = new ClkPiyasaPanel();
			clkPiyasaPanel.setBounds(145, 10, 599, 63);
			getContentPane().add(clkPiyasaPanel);
		}
		{
			lblClkLogo = new ClkLabel();
			lblClkLogo.setBounds(10, 10, 129, 65);
			getContentPane().add(lblClkLogo);
		}
	}

	// ---------------------------------------------------------------------------------

	// ---------------------------------------------------------------------------------
	private void refresh() {
		PortfoyStatics.PORTFOY.refresh();
		PortfoyStatics.PIYASA.refresh();
	}

	// ---------------------------------------------------------------------------------
	protected void do_newItemMenuItem_actionPerformed(final ActionEvent e) {
		ScrPortfoyEdit editSrc = new ScrPortfoyEdit();

		Dimension frameSize = editSrc.getSize();
		int x = this.getLocation().x + (this.getSize().width - frameSize.width) / 2;
		int y = this.getLocation().y + (this.getSize().height - frameSize.height) / 2;
		editSrc.setLocation(x, y);

		editSrc.setVisible(true);
	}

	// ---------------------------------------------------------------------------------
	protected void do_mItemHakkinda_actionPerformed(final ActionEvent e) {
		ScrAbout aboutSrc = new ScrAbout();

		Dimension frameSize = aboutSrc.getSize();
		int x = this.getLocation().x + (this.getSize().width - frameSize.width) / 2;
		int y = this.getLocation().y + (this.getSize().height - frameSize.height) / 2;
		aboutSrc.setLocation(x, y);

		aboutSrc.setVisible(true);
	}

	// ---------------------------------------------------------------------------------
	protected void do_this_windowClosing(final WindowEvent e) {
		System.exit(0);
	}

	// ---------------------------------------------------------------------------------
	protected void do_btnKapat_actionPerformed(final ActionEvent e) {
		System.exit(0);
	}

	// ---------------------------------------------------------------------------------
	protected void do_menuPortfoyDegerlendirme_actionPerformed(final ActionEvent e) {
		ScrPortfoyDetay scrPortfoyDetay = new ScrPortfoyDetay();
		scrPortfoyDetay.setLocation(this.getLocation().x, this.getLocation().y + (int) this.getBounds().getHeight());
		scrPortfoyDetay.setVisible(true);
	}

	// ---------------------------------------------------------------------------------
	protected void do_menuDegerIzleme_actionPerformed(final ActionEvent e) {
		ScrDegerIzleme scrDegerIzleme = new ScrDegerIzleme();
		scrDegerIzleme.setLocation(this.getLocation().x + (int) this.getBounds().getWidth(), this.getLocation().y);
		scrDegerIzleme.setVisible(true);
	}
	// ---------------------------------------------------------------------------------
	protected void do_newItemMenuItem_1_actionPerformed(final ActionEvent e) {
		ScrIzlemeEdit editSrc = new ScrIzlemeEdit();

		Dimension frameSize = editSrc.getSize();
		int x = this.getLocation().x + (this.getSize().width - frameSize.width) / 2;
		int y = this.getLocation().y + (this.getSize().height - frameSize.height) / 2;
		editSrc.setLocation(x, y);

		editSrc.setVisible(true);
	}

	// ---------------------------------------------------------------------------------
}